package epam.car.rent;

import java.util.regex.*;

import org.apache.log4j.Logger;

import epam.car.rent.exceptions.*;

/**
 * Describes a client entity in the system
 * 
 * @author Alexander Tretyak
 * @version 1.0
 * 
 */
public class Client extends Management{
	
	static Logger log = Logger.getLogger("carrentlogger");
	
/**
 * Pattern for validating name of the client.
 * 
 * Approves only "Name Surname" format
 */
	public static final Pattern namePattern = Pattern.compile("[\\p{L}]{2,20}\\s[\\p{L}]{3,20}");
	
	/**
	 * Pattern for validating passportID.
	 * Approves XXYYYYYY format, where XX - letters, yyyyyy - numbers
	 */
	public static final Pattern passportIdPattern = Pattern.compile("[\\p{L}]{2}[0-9]{6}");

	private String name;
	private String passportId;
	private float money;
	private String password;
	private String email;

	/**
	 * Constructor
	 * @param name 
	 * @param passportId
	 * @param money
	 * @param password
	 * @param email
	 * @throws ClientException
	 */
	public Client(String name, String passportId, float money, String password, String email) throws ClientException {
		setName(name);
		setPassportId(passportId);
		if (money >= 0)
			try {
				this.money = money;
			} catch (NumberFormatException e) {
				throw new ClientException("Field \"money\" must contain numbers only! \n");
			}
		else
			throw new ClientException("You can not set negative amount of money! \n");
		setEmail(email);
		setPassword(password);
	}
	
	/**
	 * Returns the name of the client
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * Sets the name of the client (must match the pattern - {@link Client#namePattern})
	 * @param name
	 */
	public void setName(String name) throws ClientException {
		Matcher matcher = namePattern.matcher(name);
		if (matcher.matches() == true)
			this.name = name;
		else
			throw new ClientException("The name string is incorrect! Please re-type (for example: Name Surname)\n");
	}
	
	/**
	 * returns passport id
	 */
	public String getPassportId() {
		return passportId;
	}
	public long getId() {
		return -1;
	}
	/**
	 * Sets passportID (it must match the pattern - {@link Client#namePattern})
	 * 
	 * @param passportId
	 * @throws ClientException
	 */
	public void setPassportId(String passportId) throws ClientException{
		Matcher matcher = passportIdPattern.matcher(passportId);
		if (matcher.matches() == true) this.passportId = passportId;
			else throw new ClientException("Passport ID is incorrect!\n" +
					"Please re-type (pattern is: LLNNNNNN where L - letter, N - number)\n");
	}
	
	/**
	 * returns money amount for this client
	 */
	public float getMoney() {
		return money;
	}
	
	/**
	 * Sets money amount for a client 
	 * @param money
	 */
	public void setMoney(float money) {
		this.money = money;
	}
	/**
	 * Returns only Passport ID
	 */
	public String toString(){
		return this.passportId;
	}
	/**
	 * Parametrized toString method returns full information about client delimited by tabulation
	 * @param param
	 * @return
	 */
	public String toString(String param){
		return this.name + "\t" + this.passportId + "\t" + this.money + "\t" + this.password + "\t" + this.email + "\t";
	}
	
	/**
	 * Sets password
	 * @param password
	 */
	public void setPassword(String password) {
		this.password = password;
	}
	/**
	 * Returns password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * Sets email
	 * @param email
	 */
	public void setEmail(String email) {
		this.email = email;
	}
	/**
	 * Returns email
	 */
	public String getEmail() {
		return email;
	}
}
